package com.jianyun.jyzs.fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.SelectEProjectActivity;
import com.jianyun.jyzs.adapter.SelectP2Adapter;
import com.jianyun.jyzs.bean.EngProjectLocResult;
import com.jianyun.jyzs.bean.EngineerProjectResult;
import com.jianyun.jyzs.common.ITextWatchListener;
import com.jianyun.jyzs.dao.EngLocationDao;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by yangshijun on 2018/5/31.
 */

@SuppressLint("ValidFragment")
public class P2FragmentCommon extends Fragment {


    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    Unbinder unbinder;

    private String p2id;
    private SelectP2Adapter adapter;
    private List<EngProjectLocResult.EngTypeLocBean> engineer;
    private EngLocationDao dao;
    private EngineerProjectResult.EngProject project;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_p2_all, null);
        unbinder = ButterKnife.bind(this, view);
        initData();
        return view;
    }

    private void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        dao = new EngLocationDao(getActivity());
        adapter = new SelectP2Adapter((SelectEProjectActivity) getActivity(), null, project);
        recyclerView.setAdapter(adapter);
        engineer = dao.getEngineerCommon(p2id);

        for (EngProjectLocResult.EngTypeLocBean ee:engineer
                ) {
            RceLog.d("lml",ee.getPosition());
        }
        if (engineer.size() == 0) {
            HashMap<String,String> map = new HashMap<>();
            map.put("method", "GetProJectPostionList");
            map.put("ProJectId", p2id);
            map.put("enterpriseCode", ((SelectEProjectActivity) getActivity()).enterpriseCode);
//            EngineerDatahelper.getP2Data(dao, map, new EngineerDatahelper.DataGetListener() {
//                @Override
//                public void getResult(boolean b, String msg) {
//                    if (b){
//                        engineer = dao.getEngineer(p2id);
//                        adapter.setList(engineer);
//                    }else {
//                        ToastUtil.showNoWaitToast(getActivity(),msg);
//                    }
//                }
//            });
        } else {
            adapter.setList(engineer);
        }
        searchEdit.setText("");
        searchEdit.addTextChangedListener(new ITextWatchListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                setP2View(str);
            }
        });
    }

    public P2FragmentCommon(EngineerProjectResult.EngProject project) {
        this.p2id = project.getAutoId() + "";
        this.project = project;
    }


    private void setP2View(String str) {
        if (TextUtils.isEmpty(str)) {
            engineer = dao.getEngineer(p2id);
        } else {
            engineer = dao.getEngineerSearch(str, p2id);
        }
        adapter.setList(engineer);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
